package org.raymond.icloud.modules.admin.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.raymond.icloud.apis.admin.entity.TSysUser;
import org.raymond.icloud.apis.admin.service.ITSysUserServiceApi;
import org.raymond.icloud.apis.admin.vo.LoginResponseVO;
import org.raymond.icloud.apis.admin.vo.LoginRequestVO;
import org.raymond.icloud.apis.admin.vo.UserInfoVO;
import org.raymond.icloud.apis.admin.vo.entity.SysPermissionVO;
import org.raymond.icloud.apis.admin.vo.entity.SysUserVO;
import org.raymond.icloud.common.core.bean.PageBean;
import org.raymond.icloud.common.core.model.Result;
import org.raymond.icloud.common.core.mybatis.controller.BaseController;
import org.raymond.icloud.common.core.util.RequestUtil;
import org.raymond.icloud.modules.admin.service.ITSysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

//@Api(tags = "系统用户")
@Slf4j
@RestController
public class SysUserController extends BaseController<ITSysUserService, TSysUser> implements ITSysUserServiceApi {
    @Override
    @ApiOperation(value = "创建系统用户")
    public Result<SysUserVO> create(SysUserVO vo){
        TSysUser tSysUser = TSysUser.builder().build();
        BeanUtils.copyProperties(vo, tSysUser);
        service.save(tSysUser);
        return Result.success("创建系统用户成功", vo);
    }

    @Override
    @ApiOperation(value = "根据用户名获取系统用户")
    public Result<SysUserVO> getByUsername(String username) {
        TSysUser tSysUser = service.findByUsername(username);
        SysUserVO vo = new SysUserVO();
        BeanUtils.copyProperties(tSysUser, vo);
        return Result.success("根据用户名获取系统用户成功", vo);
    }

    @Override
    @ApiOperation(value = "修改系统用户")
    public Result<SysUserVO> update(SysUserVO vo) {
        Assert.notNull(vo.getId(), "用户Id必填");
        TSysUser tSysUser = TSysUser.builder().build();
        BeanUtils.copyProperties(vo, tSysUser);
        service.updateById(tSysUser);
        return Result.success("修改系统用户成功", vo);
    }

    @Override
    @ApiOperation(value = "删除系统用户")
    public Result<Boolean> delete(SysUserVO vo){
        Assert.notNull(vo.getId(), "用户Id必填");
        Boolean result = service.removeById(vo.getId());
        return Result.success("删除系统用户"+(result?"成功":"失败"), result);
    }

    @Override
    @ApiOperation(value = "查询系统用户列表")
    public Result<PageBean<SysUserVO>> list(PageBean pageBean) {
        Page<TSysUser> page = new Page<>();
        page.setCurrent(pageBean.getCurrent());
        page.setSize(pageBean.getPageSize());
        page.setOrders(pageBean.getOrders());
        List<SysUserVO> voList = service.page(page).getRecords().stream().map(entity->{
            SysUserVO vo = new SysUserVO();
            BeanUtils.copyProperties(entity, vo);
            return vo;
        }).collect(Collectors.toList());
        pageBean.setData(voList);
        pageBean.setTotal(page.getTotal());
        return Result.success(pageBean);
    }

    @Override
    public Result<LoginResponseVO> login(LoginRequestVO loginVO) {
        log.info("user {} login by {}", loginVO.getUsername(), loginVO.getPassword());

        List<SysPermissionVO> permissionList = service.currentUserPermissions();
        String token = RequestUtil.resolve(request);
        LoginResponseVO responseVO = new LoginResponseVO();
        responseVO.setToken(token);
        responseVO.setPermissionList(permissionList);
        return Result.success(responseVO);
    }

    @Override
    public Result<UserInfoVO> current() {
        return Result.success(service.currentUserInfo());
    }

    @Override
    public Result<Boolean> logout() {
        // 清理业务数据
        return Result.success(Boolean.TRUE);
    }
}
